﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Replace : Form
    {
        public delegate void replaceDataHandler(Data data);
        public replaceDataHandler myReplaceData;
        public event replaceDataHandler replaceDataEvent;
        private Data d;
        private Data newData;
        bool validatedx;
        bool validatedy;
        bool validatedl;
        int selectedColor;
        public Replace(Data d)
        {
            this.d = d;
            //this.newData = newData;
            InitializeComponent();
            valueLabel.Text = d.label;
              valueX.Text = d.x.ToString();

              valueY.Text = d.y.ToString();
              validatedx = true;
              validatedy = true;
              validatedl = true;
              selectedColor = d.color;
              colorControl1.Color = d.color;
              colorControl1.ChangeColourEvent += new ColorControl.ChangeColourHandler(color);
        }

        private void okReplace_Click(object sender, EventArgs e)
        {
            if (!(validatedx && validatedy && validatedl))
                return;
            if (valueLabel.Text.Length > 0 && valueX.Text.Length > 0 && valueY.Text.Length > 0)
            {
                d.setParams(valueLabel.Text, int.Parse(valueX.Text), int.Parse(valueY.Text), selectedColor);
                replaceDataEvent(d);
                this.Close();
            }
        }
        private void valueX_Validating(object sender, CancelEventArgs e)
        {
            validatedx = false;
            int num;
            bool isnum = int.TryParse(valueX.Text, out num);
            if (isnum)
                errorProvider1.SetError(valueX, "");
            else
            {
                errorProvider1.SetError(valueX, "not numeric");
                e.Cancel = true;
            }
        }

        private void valueX_Validated(object sender, EventArgs e)
        {
            validatedx = true;
        }

        private void valueY_Validated(object sender, EventArgs e)
        {
            validatedy = true;
        }

        private void valueY_Validating(object sender, CancelEventArgs e)
        {
            validatedy = false;
            int num;
            bool isnum = int.TryParse(valueY.Text, out num);
            if (isnum)
                errorProvider1.SetError(valueY, "");
            else
            {
                errorProvider1.SetError(valueY, "not numeric");
                e.Cancel = true;
            }
        }

        private void valueLabel_Validating(object sender, CancelEventArgs e)
        {
            validatedl = false;
            if (valueLabel.Text.Length <= 0)
            {
                errorProvider1.SetError(valueLabel, "empty");
                e.Cancel = true;
            }
            else
            {
                errorProvider1.SetError(valueLabel, "");
            }
        }

        private void valueLabel_Validated(object sender, EventArgs e)
        {
            validatedl = true;
        }
        void color(int col)
        {
            selectedColor = col;
            colorLabel.Text = col.ToString();
        }
        private void colorControl1_Load(object sender, EventArgs e)
        {
            colorLabel.Text = colorControl1.Color.ToString();
        }
    }
}
